package com.ay.selfservicemachine.base;

import android.os.Bundle;



import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;

@SuppressWarnings("unused")
/**
 * Operations need the instance of View（exactly {@link BaseActivity} & {@link BaseFragment}）
 * can only be done after function {@link Presenter.attachView(V view)} or
 * NullPointException may occur.
 *
 * Also when view destroy (function onDestroy() is executed) , presenter will execute
 * {@link Presenter.detachView()} to detachPresenter reference of view. That means those Operations
 *  may lead NullPointException too.
 *
 *  In order to make the reference between {@link Presenter} and View as weak as possible,
 *  do not use the instance of view as any params of methods of  {@link Presenter}(especially
 *  constructor!), instead, use {@link Presenter.getView()} to get the instance
 *
 */
public abstract class Presenter<V extends IContract.IView> implements ViewPresenterConnector<V> {

    private WeakReference<V> view;

    public void initData(){

    }
    @Override
    public void attachView(V view) {
        this.view = new WeakReference<V>(view);
    }

    @Override
    public void detachView() {
//        if (view != null) {
//            view.clear();
//            view = null;
//        }
    }

    public V getView() {
        return view.get();
    }


    protected HashMap<String, Object> perPageData = new HashMap<>();
    /**
     * this method only can be executed after attachView()
     */
    public void getPrePageData() {
        if (getView() instanceof BaseActivity) {
            if (((BaseActivity) getView()).getIntent() == null) {
                return;
            }
            if (((BaseActivity) getView()).getIntent().getExtras() == null) {
                return;
            }
            Bundle bundle = ((BaseActivity) getView()).getIntent().getExtras();
            if (bundle != null) {
                Iterator<String> it = bundle.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    if (key == null) {
                        continue;
                    }
                    Object value = bundle.get(key);
                    perPageData.put(key, value);
                }
            }
        }
//        else if (getView() instanceof BaseDialogActivity) {
//            if (((BaseDialogActivity) getView()).getIntent() == null) {
//                return;
//            }
//            if (((BaseDialogActivity) getView()).getIntent().getExtras() == null) {
//                return;
//            }
//            Bundle bundle = ((BaseDialogActivity) getView()).getIntent().getExtras();
//            if (bundle != null) {
//                Iterator<String> it = bundle.keySet().iterator();
//                while (it.hasNext()) {
//                    String key = it.next();
//                    if (key == null) {
//                        continue;
//                    }
//                    Object value = bundle.get(key);
//                    perPageData.put(key, value);
//                }
//            }
//        }else if (getView() instanceof BaseFragment) {
//            if (((BaseFragment) getView()).getArguments() == null) {
//                return;
//            }
//            Bundle bundle = ((BaseFragment) getView()).getArguments();
//            if (bundle != null) {
//                Iterator<String> it = bundle.keySet().iterator();
//                while (it.hasNext()) {
//                    String key = it.next();
//                    if (key == null) {
//                        continue;
//                    }
//                    Object value = bundle.get(key);
//                    perPageData.put(key, value);
//                }
//            }
//        }
    }


//    private M module;
//
//    protected abstract M createModule();



    protected abstract void resetView();

    public  void refreshData(){}

    public abstract void initModule();

    public abstract void releaseModule();

}
